package io.github.betacatcode.influx.config;


import io.github.betacatcode.influx.ProxyMapperRegister;
import io.github.betacatcode.influx.core.Executor;
import org.influxdb.InfluxDB;
import org.influxdb.impl.InfluxDBMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.ResourcePatternResolver;

@Configuration
public class InfluxDBConfig implements EnvironmentAware {

    private Environment env;


    @Bean(name = "influxDBMapper")
    public InfluxDBMapper influxDBMapper(InfluxDB influxDB) {
        influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);
        return new InfluxDBMapper(influxDB);
    }

    @Bean(name = "executor")
    public Executor executor(InfluxDB influxDB, InfluxDBMapper influxDBMapper) {
        return new Executor(influxDB, influxDBMapper);
    }

    @Bean(name = "proxyMapperRegister")
    public ProxyMapperRegister proxyMapperRegister(ResourcePatternResolver resourcePatternResolver,
                                                   ApplicationContext applicationContext) {
        String mapperLocation = env.getProperty("spring.influx.mapper-location");
        return new ProxyMapperRegister(resourcePatternResolver,applicationContext,mapperLocation);
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }
}
